var app = new Vue({
    el: '.box',
    data: {
        currentPage: 0,
        tableData: '',
        page: '',
        input: '',
        search_flag: false,//是否搜索状态
        search_data: '',//搜索数据
    },
    methods: {
        handleSizeChange(val) {
            if (!this.search_flag) {
                select_book(val);
                this.page = val;
            } else {
                search_book_page(val);
            };
        },
        handleCurrentChange(val) {
            if (!this.search_flag) {
                select_book(val);
                this.page = val;
            } else {
                search_book_page(val);
            };

        },
        search() {
            // console.log(this.input);
            if (!this.input) {
                this.$message({
                    showClose: true,
                    message: '请输入关键字哦~',
                    type: 'warning'
                });
                return false;
            };
            search_book(this.input);
        },
        del(obj) {
            var obj = obj;
            this.$confirm('此操作将删除该订阅者, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del_book(obj, 0);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        get_emails(obj) {
            window.location.href = './set_email.html?email=' + obj;
        }
    }
});

function search_book(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    var page = app.page || 1;
    axios({
        method: "POST",
        baseURL: '../../api/subscribe.php',
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 1062, token: (sessionStorage.getItem('token')), keys: obj, flg: 1, page: page })
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.tableData = [];
            for (var i = 0; i < res.data.data.length; i++) {
                res.data.data[i].flg1 = res.data.data[i].flg - 0 ? true : false;
            };
            for (var j = 0; j < 10; j++) {
                if (res.data.data[j]) {
                    app.tableData.push(res.data.data[j]);
                };
            };

            app.search_flag = true;
            app.search_data = res.data.data;
            app.currentPage = Math.ceil(res.data.data.length / 10);
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//搜索

function search_book_page(page) {
    app.tableData = [];
    for (var j = (page - 1) * 10; j < ((page - 1) * 10 + 10); j++) {
        if (app.search_data[j]) {
            app.tableData.push(app.search_data[j]);
        };
    };
};//搜索分页



select_book(1);
function select_book(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/subscribe.php',
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 1050, token: (sessionStorage.getItem('token')), page: obj })
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.tableData = res.data.data;
            for (var i = 0; i < res.data.data.length; i++) {
                res.data.data[i].flg1 = res.data.data[i].flg - 0 ? true : false;
            };
            app.search_flag = false;
            app.currentPage = Math.ceil(res.data.all_page);
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//查询文章

function del_book(obj, flg) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/subscribe.php',
        headers: { "Content-type": "application/json; charset=utf-8" },
        data: JSON.stringify({ head: 1051, token: (sessionStorage.getItem('token')), id: obj, flg: flg })
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            select_book(1);
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            app.$message.error('操作失败~');
        };
    }).catch(function (err) {
        console.log(err);
    });
};//更改状态

